import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;

import GlobalConst from '../common/globalConst.js'
var global = GlobalConst.GlobalConst;
import LGXCOM from '../common/common.js'

import {LGXEnums}  from '../common/sysenum.js';


import { AlignItemToContainerSvc } from './dragdrop/alignItemToContainer.js';

var AbsorbItemToContainerSvc = {}


//return { offset: offsetPoint, xabsorb, yabsorb };
AbsorbItemToContainerSvc.absorb = function (pDataMng, mousePoint, originOffsetPt) {
    if (pDataMng.getMapWorkMode() != LGXEnums.LGXWorkMode.ModifyMode) {
        return null;
    }
    var pCurentGraph = pDataMng.getCurentGraph();
    if (!pCurentGraph) {
        return null;
    }
    if (!pCurentGraph.chkIsContainerItem()) {
        return null;
    }
    return this.absorbContainerItemSurround(pDataMng, pCurentGraph, mousePoint, originOffsetPt);
}

AbsorbItemToContainerSvc.absorbContainerItemSurround = function (pDataMng, pCurentGraph, mousePoint, originOffsetPt) {
    let scaling = pDataMng.getScaling();

    let destContianerGraph = null;
    let curentRect = pCurentGraph.getZone();
    var graphLst = pDataMng.getAllGraphs();
    var count = graphLst.length;
    for (let i = 0; i < count; i++) {
        let pGraph = graphLst[i];
        if (pCurentGraph == pGraph) {
            continue;
        }
        if (!this.chkCanAbsorb(pCurentGraph, pGraph)) {
            continue;
        }
        let destRect = pGraph.getZone();
        if (funcs.chkOverlap(curentRect, destRect)) {
            destContianerGraph = pGraph;
            break;
        }
    }
    if (!destContianerGraph) {
        return null;
    }

    let rstData = null;
    let verticalAlignItemGraph = destContianerGraph.findVerticalAlignItemGraph(curentRect.top);
    if (verticalAlignItemGraph) {
        let destContainerGraphRect = destContianerGraph.getZone();
        let chkGraphTopMidPt = { x: 0, y: 0 };
        chkGraphTopMidPt.x = ( + destContainerGraphRect.right) / 2;
        chkGraphTopMidPt.y = destContainerGraphRect.bottom;

        let offset = { x: 0, y: 0 };
        offset.x = destContainerGraphRect.left - curentRect.left;
        offset.y = verticalAlignItemGraph.getZone().bottom - curentRect.top;
        // mousePoint.x += offset.x;
        // mousePoint.y += offset.y;
        rstData = { offset, xabsorb: false, yabsorb: false };
        if (verticalAlignItemGraph.m_graphType == LGXEnums.LGXGraphType.containerItem_type){
            verticalAlignItemGraph.setEdgeHighLight("bottom");
        }
    }
    return rstData;
}

AbsorbItemToContainerSvc.chkCanAbsorb = function (pCurentGraph, pGraph) {
    if (funcs.mainObjType(pCurentGraph.getType()) != funcs.mainObjType(pGraph.getType())) {
        return false;
    }
    let flag = false;
    let symbol = pGraph.getSymbol();
    if (symbol) {
        let moldActionType = symbol.getMoldActionType();
        if (moldActionType == LGXEnums.moldActionType.container) {
            flag = true;
        }
    }
    return flag;
}


export default { AbsorbItemToContainerSvc }


